<template>
	<view class="route-coupon">
		<view class="inner">
			<view class="have" v-if="couponList.length">
				<view class="coupon-item " v-for="(item,index) in couponList" :key="index">
					<view class="item-info">
						<view class="bgcoupon">
							<image class="img" src="../../static/images/couponBlue.png" mode=""></image>
						</view>
						<view class="info-inner">

							<view class=" left-box">
								<view class="coupon-num-box">{{item.price}} <text>元</text> </view>
								<view class="coupon-rule">满{{item.fullprice}}元使用</view>
							</view>
							<view class="middle">
								<view class="top">
									<view class="coupon-title">{{item.name}}</view>
									<view class="disimg" v-if="item.lingqu>=item.receivenum&&item.receivenum>0">
										已领取
									</view>
								</view>
								<view class="desc">
									{{item.title}}
								</view>

							</view>
							<view class="right" v-if="item.lingqu<item.receivenum||item.receivenum===0">
								<view class="btn" @click="handleLinqu(item)">
									领取
								</view>
							</view>

						</view>

					</view>

					<view class="">

					</view>
				</view>
				<view class="footer-btn">
					<button open-type="share" class="sharebtn">
						分享
					</button>
				</view>
			</view>

			<view class="none" v-else>
				<u-empty mode="list">
				</u-empty>
			</view>


		</view>

	</view>
</template>

<script setup>
	import subScribeMsg from '@/utils/subScribeMsg.js'
	import {
		couponApi,
		userApi
	} from '@/api/index.js'
	import handleCache from "@/utils/cache.js"
	import {
		ref,
		computed
	} from 'vue'
	import {
		onShow,
		onLoad
	} from "@dcloudio/uni-app"
	const userInfo = ref('')
	const couponList = ref([])
	const shareShanghuId = ref('')
	const shareData = computed(() => { // 分享的数据

		return {
			title: `${userInfo.value.shanghuname?userInfo.value.shanghuname+'-':''}-优惠券中心`,
			path: `/pages/couponCenter/couponCenter?id=${userInfo.value.id}&shanghuid=${userInfo.value.shanghuid}&pid=${userInfo.value.id}`,
			desc: '快来看看',
		}
	})
	onShow(async () => {
		userInfo.value = handleCache.get('userInfo')
		if (shareShanghuId.value) {
			if (!handleCache.get('userInfo')) {
				uni.navigateTo({
					url: '/pages/login/login?path=/pages/couponCenter/couponCenter&shanghuId=' +
						shareShanghuId.value
				})
			} else {
				await userApi.userstore({
					uid: userInfo.value.id,
					shanghuid: shareShanghuId.value
				}).then((res) => {
					if (res.code == 0) {
						let data = {
							...userInfo.value
						}
						data.shanghuid = shareShanghuId.value
						userInfo.value = data
						handleCache.remove('userInfo')
						handleCache.set('userInfo', data, 'forever')
					}
				})
			}


		}
		await getCoupon()

		uni.$mpShare = shareData.value
	})
	onLoad((options) => {
		if (options.shanghuid) {
			shareShanghuId.value = Number(options.shanghuid)

		}
		if (options.pid) {
			getApp().globalData.shareUserId = Number(options.pid)
			if (handleCache.get('userInfo')) {
				userApi.bangdingsj({
					uid: handleCache.get('userInfo').id,
					pid: Number(options.pid)
				})
			}

		}

	})
	const getCoupon = async () => {
		let req = {
			uid: userInfo.value.id,
			shanghuid: userInfo.value.shanghuid
		}
		await couponApi.coupons(req).then((res) => {
			couponList.value = res.data
		})
	}
	const handleLinqu = async (data) => {
		await subScribeMsg(['PyJ0HcKce2xKdn_VJt0ubvg8FwAsZnKBYBRDpX1RQiQ',
			'i-zVR-Vt0LYwhg5wbln2KWFA7j74_aCjG8QBe4lazUY', 'i-zVR-Vt0LYwhg5wbln2KWFA7j74_aCjG8QBe4lazUY'
		])

		uni.showLoading({
			title: '请稍等',
			mask: true
		})
		couponApi.couponlq({
			couponid: data.id,
			uid: userInfo.value.id
		}).then((res) => {
			uni.hideLoading()
			if (res.code === 0) {
				uni.showToast({

					mask: true,
					title: '领取成功',

				})
				getCoupon()
			} else {
				uni.showToast({
					icon: 'none',
					mask: true,
					title: res.msg,

				})
				getCoupon()
			}
		})
	}
</script>

<style lang="scss" scoped>
	@import 'index.scss'
</style>